package com.imooc.newretail.bpl.ctrl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imooc.newretail.bpl.form.DeptForm;
import com.imooc.newretail.bpl.form.DeptUserForm;
import com.imooc.newretail.common.exceptions.BplException;
import com.imooc.newretail.common.utils.BeanUtils;
import com.imooc.newretail.common.vo.RV;
import com.imooc.newretail.common.vo.ResultVO;
import com.imooc.newretail.bpl.dto.DeptDTO;
import com.imooc.newretail.bpl.dto.TenantUserDTO;
import com.imooc.newretail.bpl.entity.Dept;
import com.imooc.newretail.bpl.entity.TenantUser;
import com.imooc.newretail.bpl.service.IDeptService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.imooc.newretail.common.BaseController;

/**
 * <p>
 * 部门信息 前端控制器
 * </p>
 *
 * @author Hank
 * @since 2020-06-27
 */
@Api(tags = "部门管理")
@RestController
@RequestMapping("/sys/dept")
public class DeptController extends BaseController<IDeptService> {

    @Autowired
    IDeptService deptService;


    /***
     * 新增和修改部门对象
     * @param deptForm
     * @return
     */
    @ApiOperation(value = "保存部门对象", notes = "新增或修改部门")
    @PostMapping("/")
    public ResultVO save(@RequestBody DeptForm deptForm) {
        Dept dept = BeanUtils.copyProperties(deptForm, Dept.class);
        boolean dotype = deptService.saveOrUpdate(dept);
        return dotype ? RV.success(dotype) : RV.fail("操作失败");
    }

    @ApiOperation(value = "根据ID删除部门对象", notes = "删除部门")
    @ApiImplicitParam(value = "部门ID", name = "deptId", required = true)
    @DeleteMapping("/{deptId}")
    public ResultVO deleteById(@PathVariable("deptId") String deptId) {
        boolean dotype = deptService.removeDeptAndUser(deptId);
        return dotype ? RV.success(dotype) : RV.fail("操作失败");
    }

    @ApiOperation(value = "查询对应租户下的部门列表", notes = "查询租户部门")
    @ApiImplicitParam(value = "租户ID", name = "tenantId", required = true)
    @GetMapping("/{tenantId}")
    public ResultVO<DeptDTO> getDeptsByTenantId(@PathVariable("tenantId") String tenantId) {
        QueryWrapper query = new QueryWrapper();
        query.eq("tenant_id", tenantId);
        return RV.success(deptService.list(query));
    }

    @ApiOperation(value = "添加部门用户", notes = "给指定部门添加用户")
    @PostMapping("/addUser")
    public ResultVO addUser(@RequestBody DeptUserForm deptUser) {

        TenantUser user = BeanUtils.copyProperties(deptUser, TenantUser.class);
        boolean dotype = deptService.addUser(user);
        return dotype ? RV.success(dotype) : RV.fail("操作失败");
    }

    /***
     * 查询某个租户部门下的用户列表
     * @param deptId
     * @return
     */
    @ApiOperation(value = "查询指定部门下的用户", notes = "查询指定部门下的用户")
    @ApiImplicitParam(value = "部门ID", name = "deptId", required = true)
    @GetMapping("/{deptId}users")
    public ResultVO<TenantUserDTO> getUsersByDeptId(@PathVariable("deptId") String deptId) {
        return RV.success(deptService.getUsersByDeptId(deptId));
    }

    @ApiOperation(value = "删除指定部门下的具体用户", notes = "删除指定部门下的具体用户")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "部门ID", name = "deptId", required = true),
            @ApiImplicitParam(value = "用户ID", name = "userId", required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 0,message = "操作成功"),
            @ApiResponse(code=1,message = "操作失败",response = BplException.class)
    })
    @DeleteMapping("/{deptId}/{userId}")
    public ResultVO<TenantUserDTO> removeUser(@PathVariable("deptId") String deptId, @PathVariable("userId") String userId) {
        return RV.success(deptService.removeUser(deptId, userId));
    }

}
